import re


def rj_imglist(input: list | str) -> list:
    """转换rj图片链接列表
    1. 长度大于5时，截取前5。否则用辰星logo图补齐长度
    2. 每一项转换为图片对象
    3. 返回处理结果

    Args:
        input (list|str): 原始图片列表

    Returns:
        list: [{"photo": url},xxxx]
    """

    logo_url = "https://images.rjmart.cn/image/enqu62up/860d4319-09b2-4af2-8cde-3bb94773fdee.jpg"

    # 处理字符串输入
    if isinstance(input, str):
        return [{"photo": input}] + [{"photo": logo_url}] * 4

    # 处理列表输入
    # 截取或补齐长度
    input = input[:5]
    if len(input) < 5:
        input.extend([logo_url] * (5 - len(input)))

    # 将url列表转换为图片对象
    return [{"photo": url} for url in input]


def process_img(img_list: list) -> list:
    new_list = []
    for _ in range(5):
        new_list.append(img_list[0])
    return new_list


def header_placeholder() -> str:
    """
    返回头部占位符HTML
    单行图片时,最大宽度为 950px
    单行两张图片时,图片宽度为 450px
    """
    return """
    <div class="cxsw_header_placeholder">
        <img src="https://bio-mercury-1316961827.cos.ap-guangzhou.myqcloud.com/ad/header/banner_1.png">
        <img src="https://bio-mercury-1316961827.cos.ap-guangzhou.myqcloud.com/ad/header/banner_2.png">
        <img src="https://bio-mercury-1316961827.cos.ap-guangzhou.myqcloud.com/ad/header/banner_3.png">
        <img src="https://bio-mercury-1316961827.cos.ap-guangzhou.myqcloud.com/ad/header/banner_4.png">
        <img src="https://bio-mercury-1316961827.cos.ap-guangzhou.myqcloud.com/ad/header/banner_5.png">
        <img src="https://bio-mercury-1316961827.cos.ap-guangzhou.myqcloud.com/ad/header/banner_6.png">
        <img src="https://bio-mercury-1316961827.cos.ap-guangzhou.myqcloud.com/ad/header/banner_7.png">
        <img src="https://bio-mercury-1316961827.cos.ap-guangzhou.myqcloud.com/ad/header/banner_8.png">
        <img src="https://bio-mercury-1316961827.cos.ap-guangzhou.myqcloud.com/ad/header/banner_9.png">
        <img src="https://bio-mercury-1316961827.cos.ap-guangzhou.myqcloud.com/ad/header/banner_10.png">
    </div>
    """


def add_ad_service() -> str:
    """
    添加广告服务
    """
    return """
    <div class="cxsw_service">
            <a href="https://work.weixin.qq.com/kfid/kfcd1ffef164b328abc" target="_blank" style="text-decoration:none;
    color:#333;display: flex;align-items: center;">
                <img src="https://bio-mercury-1316961827.cos.ap-guangzhou.myqcloud.com/ad/service.gif"
                    alt="点击立即咨询" srcset="">
            </a>
        </div>
    """


def footer_placeholder() -> str:
    """
    返回底部占位符HTML
    """
    return """
    <div class="cxsw_footer_placeholder">
    <img src="https://bio-mercury-1316961827.cos.ap-guangzhou.myqcloud.com/ad/footer/banner_1.png">
    <img src="https://bio-mercury-1316961827.cos.ap-guangzhou.myqcloud.com/ad/footer/banner_2.png">
    <img src="https://bio-mercury-1316961827.cos.ap-guangzhou.myqcloud.com/ad/footer/banner_3.png">
    <img src="https://bio-mercury-1316961827.cos.ap-guangzhou.myqcloud.com/ad/footer/banner_4.png">
    <img src="https://bio-mercury-1316961827.cos.ap-guangzhou.myqcloud.com/ad/footer/banner_5.png">
    <img src="https://bio-mercury-1316961827.cos.ap-guangzhou.myqcloud.com/ad/footer/banner_6.png">
    <img src="https://bio-mercury-1316961827.cos.ap-guangzhou.myqcloud.com/ad/footer/banner_7.png">
    <img src="https://bio-mercury-1316961827.cos.ap-guangzhou.myqcloud.com/ad/footer/banner_8.png">
    <img src="https://bio-mercury-1316961827.cos.ap-guangzhou.myqcloud.com/ad/footer/banner_9.png">
    <img src="https://bio-mercury-1316961827.cos.ap-guangzhou.myqcloud.com/ad/footer/banner_10.png">
    </div>
    """


def remove_html_tags(text):
    # 使用正则表达式移除所有HTML标签
    clean_text = re.sub(r"<[^>]+>", "", text)
    return clean_text


def convert_rem_to_px(html_content, rem_base=16):
    """
    将HTML内容中的rem单位转换为px单位

    参数:
        html_content: 包含rem单位的HTML内容字符串
        rem_base: 1rem对应的px值，默认为16px

    返回:
        转换后的HTML内容字符串
    """
    # 正则表达式匹配CSS中的rem单位值
    # 匹配类似: 1rem, 2.5rem, .5rem, 3rem; 等模式
    pattern = r"(\d*\.?\d+)\s*rem"

    def replace_match(match):
        """替换匹配到的rem值为px值"""
        rem_value = float(match.group(1))
        px_value = rem_value * rem_base
        # 如果是整数，直接转换为整数形式，否则保留小数
        return f"{int(px_value)}px" if px_value.is_integer() else f"{px_value}px"

    # 执行替换
    converted_content = re.sub(pattern, replace_match, html_content)
    return converted_content
